<?php
class zhiboController{
	public $userid = '';
	public $secret = '';
	public $user_unique = '';
	public $zhibo_apiurl = 'http://api.open.letvcloud.com/live/execute';
	public $dianbo_apiurl = 'http://api.letvcloud.com/open.php';
	public $zhibo_ver = '3.0';
	public $dianbo_ver = '2.0';
	
	function __construct() {
		global $config;
        $this->userid = $config['userid'];
		$this->secret = $config['secret'];
		$this->user_unique = $config['user_unique'];
    }
	
	public function zhibo($api_name,$param='', $type = "post"){
		$param['method'] = $api_name;
		return $this->_sendZhiboApi($param, $type);
	}
	
	public function dianbo($api_name,$param='', $type = "get"){
		$param['api'] = $api_name;
		return $this->_sendDianboApi($param, $type);
	}
	protected function _sendZhiboApi($param, $type = 'post'){
		global $_G;
		$param['ver'] = $this->zhibo_ver;
		$param['userid'] = $this->userid;
		$param['timestamp'] = $_G['timestamp'].'000';
		
		$param['sign'] = $this->_getSign($param);
		
		if($type == 'post'){
			$json = $this->_curlPost($this->zhibo_apiurl,$param);
		}else{
			$json = $this->_curlGet($this->zhibo_apiurl.'?'.http_build_query($param));
		}
			
		return json_decode($json,true);
	}
	protected function _sendDianboApi($param, $type = "get"){
		global $_G;
		$param['ver'] = $this->dianbo_ver;
		$param['user_unique'] = $this->user_unique;
		$param['timestamp'] = $_G['timestamp'].'000';
		$param['format'] = 'json';
		$param['sign'] = $this->_getSign($param);
		
		if($type == 'post'){
			$json = $this->_curlPost($this->dianbo_apiurl,$param);
		}else{
			$json = $this->_curlGet($this->dianbo_apiurl.'?'.http_build_query($param));
		}
		
		$res = json_decode($json,true);
		if($res['code'] == 0){
			return isset($res['data']) ? $res['data'] : true;
		}
		return false;
	}
	protected function _getSign($param){
		ksort($param);
		$String = $this->_formatBizQueryParaMap($param);
		$String = $String.$this->secret;
		$String = md5($String);
		return $String;
	}
			
	protected function _formatBizQueryParaMap($paraMap, $urlencode = ''){
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v){
			if($urlencode){
				$v = urlencode($v);
			}
			$buff .= $k . $v;
		}
		return $buff;
	}
				
	public function _curlPost($url,$data){
		if (!function_exists('curl_init')) {
			return '';
		}
		$ch = curl_init();
		$header = "Accept-Charset: utf-8";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		curl_close($ch);
		return $tmpInfo;
	}
	
	public function _curlGet($url){
		if (!function_exists('curl_init')) {
			return '';
		}
		$ch = curl_init();
		$header = "Accept-Charset: utf-8";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		
		$tmpInfo = curl_exec($ch);
		curl_close($ch);
		return $tmpInfo;
		
	}
}
?>